<?php

declare(strict_types=1);
/**
 * Created by .
 * User: luogan
 * Date: 2021/12/17
 */

namespace App\Controller\Api\v1;


use App\Controller\AbstractController;
use App\Service\UserService;
use App\Service\AdminService;
use Hyperf\Di\Annotation\Inject;
use App\Validate\Api\AdminValidate;
use App\Service\AuthMenuService;



class AdminController extends AbstractController
{

    /**
     * @Inject
     * @var AdminValidate
     */
    private $adminValidate;

    /**
     * @Inject
     * @var UserService
     */
    private $userService;

    /**
     * @Inject
     * @var AdminService
     */
    private $adminService;

    /**
     * @Inject
     * @var AuthMenuService
     */
    private $authMenuService;

    public function login():string
    {
        //验证参数
        $data = $this->parame();
        $this->adminValidate->scene(__FUNCTION__)->validate($data);
        $result = $this->userService->login($data['username'],$data['password']);
        return success('操作成功',$result);
    }

    public function logout():string
    {
        $result = $this->userService->logout();
        return success('操作成功',$result);
    }


    /**
     * 获取当前管理员菜单
     * @return array|false|mixed|string
     * @throws \HyperfExt\Jwt\Exceptions\JwtException
     */
    public function adminMenu():string
    {
        $admin = admin_id();
        $menu = $this->authMenuService->getMenuCache($admin);
        return success('操作成功',$menu);
    }

    public function list():string
    {
        $data = $this->parame();
        $this->adminValidate->scene(__FUNCTION__)->validate($data);
        $result = $this->adminService->list($data);
        return success('操作成功',$result);
    }

    public function create():string
    {
        $data = $this->parame();
        $this->adminValidate->validate($data);
        $result = $this->adminService->create($data);
        return success('操作成功',$result);
    }

    public function update():string
    {
        $data = $this->parame();
        $this->adminValidate->scene(__FUNCTION__)->validate($data);
        $result = $this->adminService->update($data);
        return success('操作成功',$result);
    }

    public function detail():string
    {
        $data = $this->parame();
        $this->adminValidate->scene(__FUNCTION__)->validate($data);
        $result = $this->adminService->detail($data);
        return success('操作成功',$result);
    }

    public function delete():string
    {
        $data = $this->parame();
        $this->adminValidate->scene(__FUNCTION__)->validate($data);
        $result = $this->adminService->delete($data);
        return success('操作成功',$result);
    }

}
